package saucaca.fragment;

import lemon.kenhnhacvn.BaseEngine;
import lemon.kenhnhacvn.MainActivity;
import lemon.kenhnhacvn.R;
import saucaca.JSON.WServiceLib;
import saucaca.Util.TagName;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.res.Resources;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;

public class AccountRegisterFragment extends Fragment {
	String TAG = AccountRegisterFragment.class.getSimpleName();
	private MainActivity mActivity = null;
	LinearLayout btnRegister, btnFogetpass;
	EditText edtUser, edtPass, edtRepass;
	Resources resources;
	String user, pass, repass;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}

	@SuppressLint("NewApi")
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
//		getActivity().getActionBar().hide();
		resources = getResources();
		View view = inflater.inflate(R.layout.account_register, container, false);
		findViewById(view);
		mActivity.myActionBar.setMenu(R.string.register, false, false);
		return view;
	}
	@Override
	public void onHiddenChanged(boolean hidden) {
		super.onHiddenChanged(hidden);
		mActivity.myActionBar.setMenu(R.string.register, false, false);
	}

	public void findViewById(View view) {
		btnRegister = (LinearLayout) view.findViewById(R.id.register_btnLinear_Register);
		btnFogetpass = (LinearLayout) view.findViewById(R.id.register_btnLinear_forgetPass);

		edtUser = (EditText) view.findViewById(R.id.register_edt_user);
		edtPass = (EditText) view.findViewById(R.id.register_edt_pass);
		edtRepass = (EditText) view.findViewById(R.id.register_edt_repass);

		btnFogetpass.setOnClickListener(clickListener);
		btnRegister.setOnClickListener(clickListener);

	}

	View.OnClickListener clickListener = new OnClickListener() {
		@Override
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.register_btnLinear_Register:

				registerAccount();
				break;
			case R.id.register_btnLinear_forgetPass:
				switchFragment(AccountForgetpassFragment.class.getName());
				break;
			default:
				break;
			}
		}
	};

	private void registerAccount() {
		user = edtUser.getText().toString();
		pass = edtPass.getText().toString();
		repass = edtRepass.getText().toString();
		if (user.equals("") || pass.equals("") || repass.equals("")) {
			TagName.showToastShort(mActivity, resources.getString(R.string.err_register_input_null));
		} else if (!pass.equals(repass)) {
			TagName.showToastShort(mActivity, resources.getString(R.string.err_repass_notmatch));
		} else {
			new GetFromServices().execute();
		}
	}

	class GetFromServices extends AsyncTask<String, String, String> {
		private ProgressDialog progressDialog = new ProgressDialog(getActivity());

		@Override
		protected String doInBackground(String... param) {
			String str = WServiceLib.register(user, pass);
			return str;
		}

		@Override
		protected void onPreExecute() {
			super.onPreExecute();
			if (!progressDialog.isShowing())
				progressDialog.show();
		}

		@Override
		protected void onPostExecute(String result) {
			super.onPostExecute(result);
			if (result != null) {
				String request = BaseEngine.getRequest(getActivity(), result);
				if (result.equals("0")) {
					if (progressDialog.isShowing())
						progressDialog.hide();

					edtUser.setText("");
					edtPass.setText("");
					edtRepass.setText("");
					dialogThongbao();
				} else
					TagName.showToastShort(getActivity(), request);

			}
		}
	}

	public void dialogThongbao() {
		AlertDialog builder = new AlertDialog.Builder(mActivity).create();
		builder.setTitle("Đăng ký tài khoản thành công!");
		builder.setMessage("Quay trở lại màn hình đăng nhập!");
		builder.setCancelable(false);
		builder.setButton(AlertDialog.BUTTON_POSITIVE, "OK", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
				switchFragment(AccountLoginFragment.class.getName());

			}
		});
		builder.setButton(AlertDialog.BUTTON_NEGATIVE, "Cancel", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
				dialog.dismiss();
			}
		});
		builder.show();
	}

	private void switchFragment(String fragment) {
		if (getActivity() == null)
			return;
		else if (getActivity() instanceof MainActivity) {
			MainActivity a = (MainActivity) getActivity();
			a.switchContent(fragment);
		}
	}

	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		if (activity instanceof MainActivity) {
			mActivity = (MainActivity) activity;
		}
	}
}
